package com.ece.mediatheque.basededonnee;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
 * Created by loutry on 10/27/13.
 */
public class CacheHelper {

    public static Uri getUri(Context context, String string)
    {
        return Uri.parse(DatabaseProvider.CONTENT
                + DatabaseProvider.getAuthority(context)
                + DatabaseProvider.SLASH + string);
    }

    public static void savePlat(Context context, String name, int price, String categorie,
                                int calorie, String picture, String description){

        ContentValues values = new ContentValues();
        values.put(DBHelper.PLAT_NAME, name);
        values.put(DBHelper.PLAT_PRICE, price);
        values.put(DBHelper.PLAT_CATEGORIE, categorie);
        values.put(DBHelper.PLAT_CALORIE, calorie);
        values.put(DBHelper.PLAT_PICTURE, picture);
        values.put(DBHelper.PLAT_DESCRIPTION, description);

        final Cursor cursor = context.getContentResolver().query(getUri(context, DBHelper.TABLE_PLAT),
                DBHelper.PARAMS_PLAT,
                DBHelper.PLAT_NAME + " = '" + name + "' AND "
                        + DBHelper.PLAT_PRICE + " = '" + price + "' AND "
                        + DBHelper.PLAT_CATEGORIE + " = '" + categorie + "'",
                null,
                null);
        try
        {
            if(cursor != null && cursor.getCount() != 0)
            {
                context.getContentResolver().update(getUri(context, DBHelper.TABLE_PLAT), values,
                        DBHelper.PLAT_NAME + " = '" + name + "' AND "
                                + DBHelper.PLAT_PRICE + " = '" + price + "' AND "
                                + DBHelper.PLAT_CATEGORIE + " = '" + categorie + "'"
                        , null);
            }
            else
            {
                context.getContentResolver().insert(getUri(context, DBHelper.TABLE_PLAT), values);
            }
        }
        finally
        {
            if(cursor != null)
                cursor.close();
        }

    }



    // TODO tout ce qu'on veut en plus :p

}
